using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UMC.Data;

namespace UMC.WorkFlow.Subassembly
{
    /// <summary>
    /// 
    /// </summary>
    [UIDesign(Name = "表单检测", Explain = "对满足条件才能进行审批命令，提交后用异常提示")]
    public class CheckMessage : UMC.WorkFlow.Responder
    {
        protected override void OnRespond(UMC.WorkFlow.InstanceEvent taskEvent)
        {
            if (CheckSQL != null && String.IsNullOrEmpty(CheckSQL.SqlText) == false)
            {
                taskEvent.OnBeginProcess += OnBeginProcess;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [Design(Caption = "检查脚本")]
        public SqlStatusSetting CheckSQL { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [Design(Caption = "提示信息", Explain = "检查脚本第一行第一列大于0时提示信息")]
        public string Message { get; set; }
        string sonsorShapeId = "ALL";
        [Design(Caption = "触发流程结点", ArgumentType = ArgumentType.Shape)]
        public string ShapeId
        {
            get
            {
                return sonsorShapeId;
            }
            set
            {
                sonsorShapeId = value;
            }
        }
        protected override void WriteProperty(TextWriter writer)
        {
            writer.Write(',');
            JSON.Serialize("ShapeId", this.ShapeId, writer);
            if (String.IsNullOrEmpty(Message) == false)
            {
                writer.Write(',');
                JSON.Serialize("Message", this.Message, writer);
            }

            if (this.CheckSQL != null)
            {
                writer.Write(',');
                JSON.Serialize("CheckSQL", this.CheckSQL, writer);
            }

            base.WriteProperty(writer);
        }
        public override Func<object> GetInstance(string prototypeName)
        {
            switch (prototypeName)
            {
                case "CheckSQL":
                    return () => new SqlStatusSetting();
                default:
                    return base.GetInstance(prototypeName);

            }
        }
        public override void Read(string key, object value)
        {
            switch (key)
            {
                case "ShapeId":
                    this.ShapeId = value as string;
                    break;
                case "Message":
                    this.Message = value as string;
                    break;
                case "CheckSQL":
                    this.CheckSQL = value as SqlStatusSetting;
                    break;

            }
            base.Read(key, value);
        }

        void OnBeginProcess(UMC.WorkFlow.TaskInstance task, UMC.WorkFlow.InstanceEventArgs e)
        {
            if (String.IsNullOrEmpty(sonsorShapeId) == false && String.Equals(sonsorShapeId, "ALL", StringComparison.CurrentCultureIgnoreCase) == false)
            {
                if (e.Shape.Id.StartsWith(sonsorShapeId) == false)
                {
                    return;
                }
                if (task.Shapes.Exists(s => s.Id.StartsWith(sonsorShapeId) && s.Status == ShapeStatus.Activation))
                {
                    return;
                }
            }
            // task.

            var es = Utility.Enum(CheckSQL.CommandType);
            for (int i = 0; i < es.Length; i++)
            {
                if (es[i] == e.CommandType)
                {

                    var obj = SubassemblyHelper.ExecuteScalar(CheckSQL, e.Runtime.Items);

                    if (obj is String)
                    {
                        throw new UMC.WorkFlow.TaskMessageException(obj as string);
                    }
                    else if (obj != null)
                    {
                        if (Utility.IntParse(obj.ToString(), 0) > 0)
                        {
                            var message = this.Message;
                            if (!String.IsNullOrEmpty(message))
                            {
                                throw new UMC.WorkFlow.TaskMessageException(message);
                            }
                        }
                    }
                }
            }
        }
    }
}
